package com.zk.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zk.yygh.dict_client.service.DictFeignClient;
import com.zk.yygh.enums.DictEnum;
import com.zk.yygh.hosp.repository.HospitalRepository;
import com.zk.yygh.hosp.service.HospitalService;
import com.zk.yygh.model.hosp.Hospital;
import com.zk.yygh.vo.hosp.HospitalQueryVo;
import com.zk.yygh.vo.order.SignInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 张凯
 * @date: 2022/6/3 - 21:16
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository repository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> parameterMap) {
        //先将参数map转为json字符串
        String parameterData = JSONObject.toJSONString(parameterMap);
        //再将json字符串转为医院对象
        Hospital hospital = JSONObject.parseObject(parameterData, Hospital.class);
        //查看mongodb里是否存有该对象
        Hospital hospitalByHoscode = repository.getHospitalByHoscode(hospital.getHoscode());
        if (hospitalByHoscode==null){
            //不存在该对象设置相关值后进行添加
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setStatus(0);
            hospital.setIsDeleted(0);
            repository.save(hospital);
        }else {
            //存在该对象就进行修改
            hospital.setCreateTime(hospitalByHoscode.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setStatus(hospitalByHoscode.getStatus());
            hospital.setIsDeleted(0);
            repository.save(hospital);
        }
        System.out.println("----->"+hospital);
    }


    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        return repository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //1.创建pageable
        Sort sort=Sort.by(Sort.Direction.DESC,"createTime");
        Pageable pageable= PageRequest.of(page-1,limit,sort);
        //2.创建实例匹配器
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example=Example.of(hospital,matcher);
        //3.执行查询
        Page<Hospital> pages = repository.findAll(example, pageable);
        //4.给hospital封装属性
        List<Hospital> hospitalList = pages.getContent();
        hospitalList.stream().forEach(item->{
            this.packHospital(item);
        });
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        //先查询医院
        Hospital hospital = repository.findById(id).get();
        if (hospital!=null){
            //设置其状态
            hospital.setStatus(status);
            repository.save(hospital);
        }
    }

    //根据id查询医院详情
    @Override
    public HashMap<String, Object> getHospitalById(String id) {
        //查询医院
        Hospital hospital = repository.findById(id).get();
        HashMap<String, Object> result = new HashMap<>();
        if (hospital!=null){
            result.put("bookingRule",hospital.getBookingRule());
            //避免重复添加预约规则
            hospital.setBookingRule(null);
            result.put("hospital",hospital);
        }
        return result;
    }

    @Override
    public List<Hospital> findHospitalByName(String name) {
        List<Hospital> hospitalList = repository.findHospitalByHosname(name);
        return hospitalList;
    }

    //根据hoscode查询医院详情
    @Override
    public HashMap<String, Object> getHospitalDetailByHoscode(String hoscode) {
        //查询医院
        Hospital hospital = repository.getHospitalByHoscode(hoscode);
        HashMap<String, Object> result = new HashMap<>();
        if (hospital!=null){
            result.put("bookingRule",hospital.getBookingRule());
            //避免重复添加预约规则
            hospital.setBookingRule(null);
            result.put("hospital",hospital);
        }
        return result;
    }


    //远程调用
    private void packHospital(Hospital item) {
        //医院类型
        String hospType = dictFeignClient.getNameByDictCodeAndValue(String.valueOf(DictEnum.HOSTYPE), Integer.parseInt(item.getHostype()));
        //各级地址
        String provinceAddress = dictFeignClient.getNameByDictCodeAndValue(Integer.parseInt(item.getProvinceCode()));
        String cityAddress = dictFeignClient.getNameByDictCodeAndValue(Integer.parseInt(item.getCityCode()));
        String districtAddress = dictFeignClient.getNameByDictCodeAndValue(Integer.parseInt(item.getDistrictCode()));
        //给其item填充参数
        item.getParam().put("hospType",hospType);
        item.getParam().put("fullAddress",provinceAddress+cityAddress+districtAddress);
    }
}
